/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   演示使用ATIM的CH3(PA03)通道输出PWM信号。PWM的频率设为200Hz
  *          PA03推动一个LED灯，实现呼吸灯的效果。
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J28
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化ATIM
    ATIM_Init();

    while (1)
    {
        ;  // 在中断服务程序中进行PWM调节，实现呼吸灯效果
    }
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置 ATIM_CH3 端口
    AFx_ATIMCH3_PA03();
    GPIO_InitStruct.Pins = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance           = SYM_ATIM;
    hatim.Init.ClockDivision = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode   = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler     = 24 - 1;
    hatim.Init.Period        = 10000 - 1;                             // 溢出周期5ms
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    hatim.OcInit.OCMode       = ATIM_OCMODE_PWM1;
    hatim.OcInit.Pulse        = 10000 - 1;
    hatim.OcInit.OCPolarity   = ATIM_OCPOLARITY_HIGH;
    hatim.OcInit.OCNPolarity  = ATIM_OCNPOLARITY_HIGH;
    hatim.OcInit.OCFastMode   = ATIM_OCFAST_DISABLE;
    hatim.OcInit.OCIdleState  = ATIM_OCIDLESTATE_RESET;
    hatim.OcInit.OCNIdleState = ATIM_OCNIDLESTATE_RESET;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_3);

    HAL_ATIM_ENABLE_IT(&hatim, ATIM_IT_SOURCE_UPDATE);                // 开启更新中断, 在中断里改变比较值
    NVIC_ClearPendingIRQ(ATIM_IRQn);
    NVIC_EnableIRQ(ATIM_IRQn);

    HAL_ATIM_PWM_Start(&hatim, ATIM_CHANNEL_3);
}


/*******************************************************************************
 * @brief  ATIM中断服务程序回调函数, 进行LED呼吸灯操作
 * @param  None
 *
 * @retval None
 */
void ATIM_IRQHandlerCallBack(void)
{
    static uint16_t duration = 10000;
    static uint8_t dir = 0;

    if (HAL_ATIM_GET_IT_SOURCE(&hatim, ATIM_IT_SOURCE_UPDATE) &&
        HAL_ATIM_GET_FLAG(&hatim, ATIM_IT_FLAG_UPDATE))
    {
        HAL_ATIM_CLR_FLAG(&hatim, ATIM_IT_FLAG_UPDATE);
        if (!dir)
        {
            duration += 30;
            if (duration >= 10000)
            {
                dir = 1;
            }
        }
        else
        {
            duration -= 30;
            if (duration <= 3000)
            {
                dir = 0;
            }
        }

        HAL_ATIM_SET_COMPARE(&hatim, ATIM_CHANNEL_3, duration);       // 修改比较值
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
